﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FrameWork.Lib
{
    public static class BaseExt
    {
        public static string BoolValueYesNo(this object pValue)
        {
            bool tBool = pValue.BoolValue();

            if (tBool)
            {
                return "Yes";
            }
            else
            {
                return "No";
            }
        }

        public static DateTime? UNIXDateValueOrNull(this string pValue)
        {
            if (pValue.HasValue())
            {
                try
                {
                    DateTime time = new DateTime(0x7b2, 1, 1, 0, 0, 0, 0);
                    return time.AddSeconds(pValue.DoubleValueOrZero()).ToLocalTime();
                }
                catch
                {
                    return null;
                }
            }
            return null;
        }

        public static void AppendLine(this System.Text.StringBuilder pStringBuilder, string format, params object[] args)
        {
            pStringBuilder.AppendLine(string.Format(format, args));
        }

        public static double? DoubleValueOrNull(this string pValue, int decimals)
        {
            double? t = pValue.DoubleValueOrNull();
            if (t != null)
            {
                t = Math.Round(t.Value, decimals);
            }
            return t;
        }

        public static string Left(this string pValue, int pMaxLength)
        {
            if (pValue == null) return "";
            if (pValue.Length > pMaxLength)
            {
                pValue = pValue.Substring(0, pMaxLength);
            }
            return pValue;
        }

        public static string LeftOrNull(this string pValue, int pMaxLength)
        {
            if (pValue == null) return null;
            if (pValue.Length > pMaxLength)
            {
                pValue = pValue.Substring(0, pMaxLength);
            }
            return pValue;
        }

        public static string Right(this string pValue, int pMaxLength)
        {
            if (pValue == null) return "";
            if (pValue.Length > pMaxLength)
            {
                pValue = pValue.Substring(pValue.Length - pMaxLength);
            }
            return pValue;
        }

        public static string RightOrNull(this string pValue, int pMaxLength)
        {
            if (pValue == null) return null;
            if (pValue.Length > pMaxLength)
            {
                pValue = pValue.Substring(pValue.Length - pMaxLength);
            }
            return pValue;
        }

        public static System.DateTime? DateValueOrNull(this string pValue)
        {
            System.DateTime tVal;
            if (System.DateTime.TryParse(pValue, out tVal))
            {
                return tVal;
            }
            else
            {
                return null;
            }

        }

        public static DateTime? StripSecondsAndMilliseconds(this DateTime? pDateTime)
        {
            if (pDateTime == null)
            {
                return null;
            }
            else
            {
                return pDateTime.Value.StripSecondsAndMilliseconds();
            }
        }

        public static DateTime StripSecondsAndMilliseconds(this DateTime pDateTime)
        {
            return pDateTime.Date.AddHours(pDateTime.Hour).AddMinutes(pDateTime.Minute);// .AddSeconds(-pDateTime.Second).AddMilliseconds(-pDateTime.Millisecond);
        }

        public static System.DateTime ReplaceTimeValue(this DateTime pDateValue, DateTime pTimeValue)
        {
            return pDateValue.Date + pTimeValue.TimeOfDay;
        }

        public static System.DateTime? ReplaceTimeValueOrNull(this string pDateValue, string pTimeValue)
        {
            return pDateValue.DateValueOrNull().ReplaceTimeValueOrNull(pTimeValue.DateValueOrNull());
        }

        public static System.DateTime? ReplaceTimeValueOrNull(this DateTime? pDateValue, DateTime? pTimeValue)
        {
            if (pDateValue.HasValue && pTimeValue.HasValue)
            {
                return pDateValue.Value.Date + pTimeValue.Value.TimeOfDay;
            }
            else
            {
                return null;
            }

        }

        public static System.DateTime DateValueOrMin(this string pValue)
        {
            System.DateTime tVal;
            if (System.DateTime.TryParse(pValue, out tVal))
            {
                return tVal;
            }
            else
            {
                return DateTime.MinValue;
            }

        }

        public static long? LongValueOrNull(this string pValue)
        {
            long tVal;
            if (long.TryParse(pValue, out tVal))
            {
                return tVal;
            }
            else
            {
                return null;
            }

        }

        public static bool BoolValue(this object pValue)
        {
            return pValue.StringValueOrEmpty().BoolValue();

        }

        public static bool BoolValue(this string pValue)
        {
            if (pValue == null || pValue == string.Empty || pValue.Trim().Length == 0) { return false; }
            bool tOut = false;
            if (bool.TryParse(pValue, out tOut))
            {
                return tOut;
            }
            else
            {
                if (pValue.ToUpper() == "Y" || pValue.ToUpper() == "YES" || pValue.ToUpper() == "1" || pValue.ToUpper() == "-1" || pValue.ToUpper() == "T" || pValue.ToUpper() == "TRUE")
                    return true;
                else if (pValue.ToUpper() == "N" || pValue.ToUpper() == "NO" || pValue.ToUpper() == "0" || pValue.ToUpper() == "F" || pValue.ToUpper() == "FALSE")
                    return false;
                else
                    throw new Exception("Invalid value for boolean type: " + pValue + ".");
            }

        }

        public static bool? BoolValueOrNull(this string pValue)
        {
            bool tOut = false;
            if (bool.TryParse(pValue, out tOut))
            {
                return tOut;
            }
            else
            {
                if (pValue.ToUpper() == "Y" || pValue.ToUpper() == "YES" || pValue.ToUpper() == "1" || pValue.ToUpper() == "T" || pValue.ToUpper() == "TRUE")
                    return true;
                else if (pValue.ToUpper() == "N" || pValue.ToUpper() == "NO" || pValue.ToUpper() == "0" || pValue.ToUpper() == "F" || pValue.ToUpper() == "FALSE")
                    return false;
                else
                    return null;
            }

        }

        public static short? ShortValueOrNull(this string pValue)
        {
            short tVal;
            if (short.TryParse(pValue, out tVal))
            {
                return tVal;
            }
            else
            {
                return null;
            }

        }

        public static bool IsNumeric(this string pValue)
        {
            double tResult = 0;
            return double.TryParse(pValue, out tResult);
        }

        public static bool IsEmpty(this object pValue)
        {

            if (pValue == null || pValue.Equals(string.Empty) || DBNull.Value.Equals(pValue))
            {
                return true;
            }
            else
            {
                return pValue.ToString().Trim().Length == 0;
            }
        }

        public static bool HasValue(this object pValue)
        {

            return !IsEmpty(pValue);
        }

        public static int? IntValueOrNull(this string pValue)
        {
            int tVal;
            if (IsNumeric(pValue))
            {
                tVal = Convert.ToInt32(DecimalValueOrNull(pValue));
                return tVal;
            }
            else
            {
                return null;
            }
        }

        public static int IntValueOrZero(this string pValue)
        {
            int tVal;
            if (int.TryParse(pValue, out tVal))
            {
                return tVal;
            }
            else
            {
                return 0;
            }
        }

        public static int IntValueOrDefault(this string pValue, int pDefaultValue)
        {
            int tVal;
            if (int.TryParse(pValue, out tVal))
            {
                return tVal;
            }
            else
            {
                return pDefaultValue;
            }
        }

        public static double? DoubleValueOrNull(this string pValue)
        {
            double tVal;
            if (double.TryParse(pValue, out tVal))
            {
                return tVal;
            }
            else
            {
                return null;
            }

        }

        public static double DoubleValueOrZero(this string pValue)
        {
            double tVal;
            if (double.TryParse(pValue, out tVal))
            {
                return tVal;
            }
            else
            {
                return 0;
            }

        }

        public static decimal? DecimalValueOrNull(this string pValue)
        {
            decimal tVal;
            if (decimal.TryParse(pValue, out tVal))
            {
                return tVal;
            }
            else
            {
                return null;
            }

        }

        public static long LongValueOrZero(this string pValue)
        {
            long tVal;
            if (long.TryParse(pValue, out tVal))
            {
                return tVal;
            }
            else
            {
                return 0;
            }

        }

        public static decimal DecimalValueOrZero(this string pValue)
        {
            decimal tVal;
            if (decimal.TryParse(pValue, out tVal))
            {
                return tVal;
            }
            else
            {
                return 0;
            }

        }

        public static short ShortValueOrZero(this string pValue)
        {
            short tVal;
            if (short.TryParse(pValue, out tVal))
            {
                return tVal;
            }
            else
            {
                return 0;
            }

        }

        public static string StringValueOrEmpty(this object pValue)
        {
            if (pValue == null)
            {
                return "";
            }

            return pValue.ToString();
        }

        public static string StringValueOrEmpty(this object pValue, int pMaxLength)
        {
            if (pValue == null)
            {
                return "";
            }

            var t = pValue.ToString();
            if (t.Length > pMaxLength)
            {
                return t.Substring(0, pMaxLength);
            }
            else
            {
                return t;
            }
        }

        public static string GetValueOrDefault(this object pValue, string pDefault)
        {
            if (pValue == null)
            {
                return pDefault;
            }

            return pValue.ToString();
        }

        public static string StringValueOrEmpty(this double? pValue)
        {

            if (pValue == null)
            {
                return "";
            }
            else
            {
                return pValue.Value.ToString();
            }

        }

        public static string StringValueOrEmpty(this DateTime? pValue)
        {
            return pValue.StringValueOrEmpty(null);
        }

        public static string StringValueOrEmpty(this DateTime? pValue, string pFormat)
        {

            if (pValue == null)
            {
                return "";
            }
            else
            {
                if (pFormat != null)
                    return pValue.Value.ToString(pFormat);
                else
                    return pValue.Value.ToString();
            }

        }
 
        public static string TruncateFromLeft(this string pValue, int pLength)
        {
            string tSubstring = "";
            if (pValue != null)
            {
                if (pValue.Length > pLength)
                {
                    tSubstring = pValue.Substring(0, pLength);
                }
            }
            return tSubstring;
        }

        public static uint UIntValueOrDefault(this string pValue, uint pDefaultValue)
        {
            uint num;
            if (uint.TryParse(pValue, out num))
            {
                return num;
            }
            return pDefaultValue;
        }

        public static string GetValueByIndex(this string[] pArray, int pIndex)
        {
            return pArray.GetValueByIndex(pIndex, "");
        }

        public static string GetValueByIndex(this string[] pArray, int pIndex, string pDefaultValue)
        {
            if (pArray == null || pArray.Length == 0 || pArray.Length <= pIndex) return pDefaultValue;

            return pArray[pIndex].StringValueOrEmpty();
        }

        public static T ToEnum<T>(this string pEnumString)
        {
            return (T)Enum.Parse(typeof(T), pEnumString);
        }

 



    }
}


